(function () {
    function build_tree(it, open, close) {
        $(it + ' li:has(ul)').addClass('parent_li').find(' > span').attr('title', '收起');
        var node = $(it + ' li.parent_li > span');
        node.each(function () {
            var children = $(this).parent('li.parent_li').find(' > ul > li');
            children.hide()
        });
        node.on('click', function (e) {
            var children = $(this).parent('li.parent_li').find(' > ul > li');
            if (children.is(":visible")) {
                children.hide('fast');
                $(this).attr('title', '展开').find(' > i').addClass(close).removeClass(open);
            } else {
                children.show('fast');
                $(this).attr('title', '收起').find(' > i').addClass(open).removeClass(close);
            }
            e.stopPropagation();
        });
    }

    function convArr(Items) {
        var arr = [];
        var i = 0;
        for (var key in Items) {
            arr[i] = [key, Items[key]];
            ++i;
        }
        return arr
    }

    function sortFolderItems(a, b) {
        return b[1]['type'] - a[1]['type']
    }

    function sortTimeItems(a, b) {
        return b[0] - a[0]
    }

    function treeFolderIt(name, item, parent) {
        var li = $('<li/>');
        var span = $('<span/>');
        if (item['type'] == 1) {
            span.text(name);
            span.prepend('<i class="uk-icon-folder-o"></i> ');
            li.append(span);
            var ul = $('<ul/>');
            var arr = convArr(item['items']).sort(sortFolderItems);
            for (var i in arr) {
                treeFolderIt(arr[i][0], arr[i][1], ul)
            }
            li.append(ul);
        }
        else {
            span.prepend('<i class="uk-icon-file-text-o"></i> ');
            span.append($("<a/>", {
                href: '/article/' + item['a'],
                text: name,
                target: '_blank'
            }));
            li.append(span);

        }
        parent.append(li)
    }

    function treeTimeIt(name, item, parent) {
        var li = $('<li/>');
        var span = $('<span/>');
        if (item['type'] == 1) {
            span.text(name + " " + item['tip']);
            span.prepend('<i class="uk-icon-calendar"></i> ');
            li.append(span);
            var ul = $('<ul/>');
            var arr = convArr(item['items']).sort(sortTimeItems);
            for (var i in arr) {
                treeTimeIt(arr[i][0], arr[i][1], ul)
            }
            li.append(ul);
        }
        else {
            span.prepend('<i class="uk-icon-file-text-o"></i> ');
            span.append($("<a/>", {
                href: '/article/' + item['a'],
                text: name,
                target: '_blank'
            }));
            li.append(span);
        }
        parent.append(li)
    }

    //var data_folder_tree = '{% raw folder_tree %}';
    var data_folder_tree = $("#folder_tree_data").text();
    data_folder_tree = JSON.parse(data_folder_tree);

    //var data_time_tree = '{% raw time_tree %}';
    var data_time_tree = $("#time_tree_data").text();
    data_time_tree = JSON.parse(data_time_tree);

    var folder_tree = $('#folder_tree');
    var time_tree = $('#time_tree');
    var folder_ul = $('<ul/>');
    var time_ul = $('<ul/>');

    data_folder_tree = convArr(data_folder_tree).sort(sortFolderItems);
    for (var i in data_folder_tree) {
        treeFolderIt(data_folder_tree[i][0], data_folder_tree[i][1], folder_ul);
    }
    folder_tree.append(folder_ul);


    data_time_tree = convArr(data_time_tree).sort(sortTimeItems);
    for (var i in data_time_tree) {
        treeTimeIt(data_time_tree[i][0], data_time_tree[i][1], time_ul);
    }
    time_tree.append(time_ul);

    build_tree('#folder_tree', 'uk-icon-folder-open-o', 'uk-icon-folder-o');
    build_tree('#time_tree', 'uk-icon-calendar-check-o', 'uk-icon-calendar');
})();
